var SS = function() {
	var colIndex = 0;
	var createTable = function(cwmx) {
		if (cwmx.points == null) {
			return;
		}
		$("#cwmxSSView").empty();

		$("#sscanghao").text(cwmx.storehousecode + "号仓");
		$("#sschecktime").text("检测时间：" + cwmx.checktime);

		$("#exportBtn").attr(
			"href",
			"/excel?orgcode=" + orgcode + "&housecode="
			+ cwmx.storehousecode);

		var $table = $("<table class='cwmx view'></table>");
		var firstRow = "<tr class='cang'>";
		firstRow = firstRow + "<td class='first' style='border-top:1px solid #000' colspan='" + (cwmx.points[0].length + 1) + "'>" + " 测温明细" + "</td>";
		firstRow = firstRow + "</tr>"
		$table.append($(firstRow));
		var points = cwmx.points;
		var alarmtemp = cwmx.alarmtemp;
		for (var i = 0; i < points.length; i++) {
			var alltemp = "0";
			var index = 0;
			for (var k = 0; k < points[i][0].length; k++) {
				if (k == 0) {
					var titleRow = "<tr class=\"hang\"><td>缆号</td>";
					for (var j = 0; j < points[i].length; j++) {
						if (points[i][j][k].lan == null) {
							titleRow = titleRow + "<td>" + "-" + "</td>";
						} else {
							titleRow = titleRow + "<td>" + points[i][j][k].lan + "号</td>";
						}
					}
					titleRow = titleRow + "</tr>";
					$table.append($(titleRow));
				}

				var dataRow = "<tr><td>" + (k + 1) + "层</td>"
				for (var j = 0; j < points[i].length; j++) {
					if (points[i][j][k].temp * 1 > alarmtemp) {
						dataRow = dataRow + "<td style=\"color:yellow;\">" + points[i][j][k].temp + "</td>";
					} else {
						if (points[i][j][k].temp * 1 == cwmx.highttemp * 1) {
							dataRow = dataRow + "<td style=\"color:red;font-weight:bold;\">" + points[i][j][k].temp + "</td>";
						} else if (points[i][j][k].temp * 1 == cwmx.lowertemp * 1) {
							dataRow = dataRow + "<td style=\"color:blue;font-weight:bold;\">" + points[i][j][k].temp + "</td>";
						} else {
							dataRow = dataRow + "<td>" + points[i][j][k].temp + "</td>";
						}
					}
					index = index + 1;
					alltemp = alltemp * 1 + points[i][j][k].temp * 1;
				}
				dataRow = dataRow + "</tr>";
				$table.append($(dataRow));
			}
		}
		$("#cwmxSSView").append($table);
		if (colIndex != 0) {
			if (colIndex % 2 == 0) {
				$("#cwmxSSView").animate({
					backgroundColor : '#ccffcc'
				}, 500);
			}
			if (colIndex % 2 == 1) {
				$("#cwmxSSView").animate({
					backgroundColor : '#ccffff'
				}, 500);
			}
		}
	}
	var getCwmx = function(storehousecode) {
		$.ajax({
			type : "GET",
			cache : false,
			url : "lq/XQlist?orgcode=" + orgcode + "&storehousecode=" + storehousecode,
			success : function(responseJson) {
				if (responseJson.success == true) { // 返回true
					createTable(responseJson.obj);
				} else {
					QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				QTYYComponents.bootstrapSweetAlert("", "查询失败!!!", "error");
			}
		});
	}
	/**
	 * 绑定按钮事件
	 */
	var eventHandler = function() {
		$("#searchSSBtn").on("click", function() {
			if (g_intervalId != null) {
				window.clearInterval(g_intervalId);
			}
			g_intervalId = window.setInterval(function() {
				colIndex++;
				getCwmx($("input[name=storehousecode]").val());
			}, $("#sjjgSS").val() * 1000);
		});
		$("#printBtn").on("click", function() {
			$("#cwmxSSView").jqprint();
		});
	}
	return {
		init : function() {
			eventHandler();
			QTYYComponents.initSelect("", "sjjgSS");
			getCwmx($("input[name=storehousecode]").val());

		}
	}
}();

jQuery(document).ready(function() {
	SS.init();
});